import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../views/index'
import store from '../store/index'
import {getStatus} from "../utils/getStatus"
Vue.use(VueRouter)

  const routes = [
    {
      path: '/',
      redirect: '/index',

    },
  {
    path: '/index',
    name: 'index',
    component: index
  },
  {
    path: '/detail/:courseId',
    name: 'detail',
    component: () => import('../views/detail.vue')
  },
    {
      path: '/shopcar',
      name: 'shopcar',
      component: () => import('../views/shopcar.vue'),
      meta :{
        islogin: true
      }
    }
]
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
  routes
})
let get = getStatus();
router.beforeEach((to,from, next) => {

      if(to.meta.islogin) {
        get.then(res => {
          console.log(res)
          if (res.data.code == 0) {
            next();
          } else {
            next("/index")
            store.commit('change', true)
          }
        })
      }else {
        next()
      }



})

export default router
